layui.use(['form', 'layer'], function () {
  $ = layui.jquery;
  var form = layui.form
      , layer = layui.layer;

  //自定义验证规则
  form.verify({
    ip: function (value) {
      if (value.length < 1) {
        return '不能为空';
      }
    }
    , name: [/(.+){2,12}$/, '不能为空']
  });

  //监听提交
  form.on('submit(add)', function (data) {
    console.log(data.field.name);

    //提交信息
    $.post({
      url: "/pt/scenic",
      data: JSON.stringify(data.field),
      contentType: "application/json",
      dataType: "json",
      complete: function (msg) {
        layer.alert("增加成功", {icon: 6},function () {
          parent.layui.table.reload('tableId', {
            page: {
              curr: 1 //重新从第 1 页开始
            }
          });
          // 获得frame索引
          var index = parent.layer.getFrameIndex(window.name);
          //关闭当前frame
          parent.layer.close(index);
        });
      }
    });
    return false;
  });

  //监听提交
  form.on('submit(edit)', function (data) {
    console.log(data.field.name);
    //提交信息
    $.ajax({
      type: "PUT",
      url: "/pt/scenic",
      data: JSON.stringify(data.field),
      contentType: "application/json",
      dataType: "json",
      complete: function (msg) {
        layer.alert("更新成功！", {icon: 6},function () {
          // 获得frame索引
          var index = parent.layer.getFrameIndex(window.name);
          //关闭当前frame
          parent.layer.close(index);
        });
      }
    });
    return false;
  });
})